package com.zy.datareceiver.binsess.rabbit.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class LoopQueue<E> implements Queue<E> {

    private E[] data;
    private int size;
    private int front;
    private int tail;

    public LoopQueue(int capacity) {
        data = (E[]) new Object[capacity + 1];
        front = 0;
        tail = 0;
        size = 0;
    }

    public LoopQueue() {
        this(10);
    }

    @Override
    public int getSize() {
        return 0;
    }

    public List<E> offer(E e) {
        enqueue(e);
        return getData(20);
    }

    public int getCapacity() {
        return data.length - 1;
    }

    public boolean isEmpty() {
        return front == tail;
    }

    public void enqueue(E e) {
        if ((tail + 1) == front)
            removeFirst();
        data[tail] = e;
        tail = (tail + 1) % data.length;
        size++;
    }

    public E removeFirst() {
        E ret = data[front];
        data[front] = null;
        front = (front + 1) % data.length;
        size--;
        return ret;
    }

    public E dequeue() {

        if (isEmpty())
            throw new IllegalArgumentException("Cannot dequeue from an empty queue.");

        E ret = data[front];
        data[front] = null;
        front = (front + 1) % data.length;
        size--;
        if (size == getCapacity() / 4 && getCapacity() / 2 != 0)
            resize(getCapacity() / 2);
        return ret;
    }

    private void resize(int newCapacity) {

        E[] newData = (E[]) new Object[newCapacity + 1];
        for (int i = 0; i < size; i++)
            newData[i] = data[(i + front) % data.length];

        data = newData;
        front = 0;
        tail = size;
    }

    public E getFront() {
        if (isEmpty())
            throw new IllegalArgumentException("Queue is empty.");
        return data[front];
    }

    public E getTail() {
        if (isEmpty())
            throw new IllegalArgumentException("Queue is empty.");
        return data[tail];
    }

    @Override
    public List<E> getData(int capacity) {
        List<E> res = new ArrayList<>(capacity);
        for (E e : data) {
            if (e == null) {
                continue;
            }
            if (res.size() >= capacity) {
                return res;
            }
            res.add(e);
        }
        return res;
    }


    public static void main(String[] args) {
        HashMap<String, HashMap<String, LoopQueue<DataGram>>> res = new HashMap<>();
        for (int k = 0; k < 2; k++) {
            HashMap<String, LoopQueue<DataGram>> deviceTypeMap = new HashMap<>();
            for (int i = 0; i < 10; i++) {
                LoopQueue<DataGram> data = new LoopQueue<DataGram>();
                Long start = System.currentTimeMillis();
                for (int j = 0; j < 10; j++) {
                    DataGram user = new DataGram();
                    List<DataGram> users = data.offer(user);
                }
                deviceTypeMap.put(i + "", data);
            }
            res.put("应变" + k, deviceTypeMap);
        }
    }
}
